package com.fitness.front.aop.dw;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.fitness.front.aop.LogMessage;
import com.fitness.front.modal.member.dto.RegisterFormDto;
import com.fitness.front.modal.member.entity.Member;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Aspect
@Component
public class RegisterLogAspect {

    @Pointcut("execution(* com.fitness.front.service.impl.MemberServiceImpl.register(..))")
    public void registerLogPointcut(){}

    @Around(value="registerLogPointcut()")
    public Object registerLog(ProceedingJoinPoint point) {
        try {
            Object[] args = point.getArgs();
            RegisterFormDto registerFormDto = (RegisterFormDto) args[0];
            Object result = point.proceed();
            Member member = new Member();
            member.setUsername(registerFormDto.getUsername());
            member.setAge(registerFormDto.getAge());
            member.setGender(registerFormDto.getGender());
            LogMessage<Member> logMessage = new LogMessage<>(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),
                    LogMessage.REGISTER_TYPE,member);
            log.info("{}", JSONUtil.toJsonStr(logMessage));
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
